package com.chen.blog.service.impl;

import com.chen.blog.common.api.CommonResult;
import com.chen.blog.model.Comment;
import com.chen.blog.mapper.CommentMapper;
import com.chen.blog.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chen
 * @since 2022-01-24
 */
@Service
@Transactional
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 检索指定博客下的评论功能实现
     *
     * @param id
     * @param currentPage
     * @param pageSize
     * @param map
     * @return
     */
    @Override
    public CommonResult getCommentByBlogId(Integer id, Integer currentPage, Integer pageSize, Map<String, Object> map) {
        if (currentPage <= 0) {
            currentPage = 0;
        } else {
            currentPage *= pageSize;
        }
        Map<String, Object> result = new HashMap<>();
        result.put("total", commentMapper.getCommentCountByBlogId(id, map));
        result.put("comment", commentMapper.getCommentByBlogId(id, currentPage, pageSize, map));
        return CommonResult.success(null, result);
    }
}
